package com.xbh.management.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbh.common.utils.JwtUtil;
import com.xbh.common.utils.JwtUtils;
import com.xbh.management.entity.ManUserVO;
import com.xbh.management.mapper.UserMapper;
import com.xbh.management.model.ManUser;
import com.xbh.management.model.ManUserRole;
import com.xbh.management.task.ExportFactory;
import com.xbh.management.task.SuperExport;
import com.xbh.management.task.impl.CustomerExportImpl;
import com.xbh.management.task.impl.UserExportImpl;
import com.xbh.management.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: management-center
 * @description:
 * @author: 许宝华
 * @create: 2021-08-29 19:33
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, ManUser> implements UserService {

    @Autowired
    private UserRoleService userRoleService;
    /**
     *
     * @param manUser
     * @return
     */
    @Override
    public ManUser selectUser(ManUser manUser) {
        LambdaQueryWrapper<ManUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ManUser::getUsername,manUser.getUsername())
                .eq(ManUser::getPassword,manUser.getPassword());
        ManUser user = this.baseMapper.selectOne(wrapper);
        return user;
    }

    /**
     *
     * @param username
     * @param imageURl
     * @return
     */
    @Override
    public int updateUserWithUsername(String username,String imageURl) {

        return this.baseMapper.updateUserByUsername(username, imageURl);

    }

    /**
     * 获取指定用户头像地址
     * @param username
     * @return
     */
    @Override
    public String queryUserWithUsername(String username) {
        LambdaQueryWrapper<ManUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ManUser::getUsername,username);
        ManUser user = this.baseMapper.selectOne(wrapper);
        return user.getHandleImage();
    }

    /**
     * 查出所有用户，支持用户姓名模糊查询
     * @param page
     * @param user
     * @return
     */
    @Override
    public IPage<ManUser> queryAllUser(Page<ManUser> page, ManUser user) {
        Page<ManUser> userPage = this.baseMapper.queryAllUserReturnPage(page, user);
        return userPage;
    }

    /**
     *
     * @param user
     * @return
     */
    @Override
    public int saveUser(ManUser user) {
        //密码加密
        String password = MD5Utils.encrypt(user.getPassword());
        user.setPassword(password);
        int isFlag = this.baseMapper.insert(user);
        //保存用户与角色关系
        ManUserRole manUserRole = new ManUserRole();
        manUserRole.setUserId(user.getUserId());
        manUserRole.setRoleId(Long.parseLong(user.getDescription()));
        manUserRole.setCreatePerson(JwtUtil.getUsername());
        manUserRole.setUpdatePerson(JwtUtil.getUsername());
        userRoleService.save(manUserRole);
        return isFlag;
    }

    /**
     * 根据id查询用户
     * @param userId
     * @return
     */
    @Override
    public ManUser queryUserWithUserId(Long userId) {
        ManUser user = this.baseMapper.selectById(userId);
        return user;
    }

    /**
     * 修改用户信息
     * @param userId
     * @return
     */
    @Override
    public int updateUserWithUserId(ManUser user) {
        int isFlag = this.baseMapper.updateById(user);
        return isFlag;
    }

    /**
     * 修改密码
     * @param user
     * @return
     */
    @Override
    public int updatePasswordWithUserId(ManUser user) {
        //密码加密
        String password = MD5Utils.encrypt(user.getPassword());
        user.setPassword(password);
        return this.baseMapper.updateById(user);
    }

    @Override
    public String exportUser(List<ManUser> userList) throws Exception {
        String result = "";

        //用户导出标题
        String[] headers = { "序号", "账户名称", "创建时间", "用户姓名", "用户电话", "用户邮箱","用户生日","类型","状态"};
        //长度设置
        Integer[] user = { 15, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35};
        //导出
        SuperExport superExport = ExportFactory.getSuperExport(UserExportImpl.class, "用户列表", headers, user, userList);
        result = superExport.call();
        return result;
    }

    /**
     * 查询当前用户所拥有的角色
     * @param user
     * @return
     */
    @Override
    public ManUserVO queryUserRelationAllRole(ManUser user) {
        ManUserVO manUserVO = this.baseMapper.queryUserRelationAllRole(user);

        return manUserVO;
    }

    @Override
    public ManUserVO queryUserRelationAllRoleByUsername(ManUser user) {
        return this.baseMapper.queryUserRelationAllRoleByUsername(user);
    }

    @Override
    public ManUser queryUser(String username) {

        QueryWrapper<ManUser> wrapper = new QueryWrapper<ManUser>();
        wrapper.eq("username", username).or().eq("telephone",username);
        return this.baseMapper.selectOne(wrapper);
    }

    @Override
    public ManUserVO queryUserRelationAllMenu(ManUser user) {
        return this.baseMapper.queryUserRelationAllMenu(user);
    }

    @Override
    public List<ManUser> queryUserByUsername(String username) {
        LambdaQueryWrapper<ManUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ManUser::getDeleted,new Integer(0));
        wrapper.like(ManUser::getPersonName,username);
        List<ManUser> manUsers = this.baseMapper.selectList(wrapper);
        return manUsers;
    }
}
